import { addEvent } from './event';

function updateProps(dom, oldProps, props) {
  for (let key in props) {
    if (key !== 'children') {
      if (key === 'style') {
        const styleObj = props[key];
        for (let styleKey in styleObj) {
          dom.style[styleKey] = styleObj[styleKey];
        }
      } else if (key.startsWith('on')) {
        addEvent(dom, key.toLowerCase(), props[key]);
      } else {
        if (key == 'number') {
          console.log(props);
        }
        try {
          dom[key] = props[key];
        } catch (error) {}
      }
    }
  }
}

function isFunction(func) {
  return typeof func === 'function';
}

function shallowEquals(obj1, obj2) {
  if (obj1 === obj2) {
    return true;
  }
  if (
    typeof obj1 !== 'object' ||
    obj1 === null ||
    typeof obj2 !== 'object' ||
    obj2 === null
  ) {
    return false;
  }
  const keys1 = Object.keys(obj1);
  const keys2 = Object.keys(obj2);
  if (keys1.length !== keys2.length) {
    return false;
  }
  for(let key of keys1) {
    if (!obj2.hasOwnProperty(key) || obj1[key] !== obj2[key]) {
      return false;
    }
  }
  return true;
}
export { updateProps, isFunction, shallowEquals };
